<?php

namespace km\log;

/**
 * Logger
 *
 * @author vbolshov
 */
abstract class Logger {
	private $respectTrace = false;
	protected $messages = array();
	function write($message, $finalize = false)
	{
		$this->messages[] = $this->toString($message);
		if ($finalize) {
			$this->finalize();
		}
	}
	protected function toString($message)
	{
		if ($message instanceof \Exception) {
			$messageString = get_class($message) . ': ' . $message->getMessage();
			if ($this->respectTrace) {
				$messageString .= ". Trace:\n" . $message->getTraceAsString();
			}
		} else {
			$messageString = $message;
		}
		return $messageString;
	}
	function setRespectTrace($boolean)
	{
		$this->respectTrace = $boolean;
	}
	function getMessageString()
	{
		return "[" . date("Y-m-d H:i:s") . "] " . str_replace("\n", "\n\t", implode("\n\t", $this->messages)) . "\n";
	}
	final function finalize()
	{
		$this->doFinalize();
		$this->messages = array();
	}
	abstract protected function doFinalize();
}